package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMchntApplyMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntApplyMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntApplyInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.realize.PublicDealService;
import cc.rengu.igas.bsps.core.realize.impl.PublicDealServiceImpl;
import cc.rengu.igas.bsps.core.service.base.BankManagerService;
import cc.rengu.igas.bsps.facade.bean.NeedExpandMchntInfoBean;
import cc.rengu.igas.bsps.facade.request.NeedExpandMchntListQueryRequest;
import cc.rengu.igas.bsps.facade.response.NeedExpandMchntListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 待拓展商户列表查询
 * Created by 王成 on 2020/3/23
 */
public class NeedExpandMchntListQueryService extends BankManagerService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        NeedExpandMchntListQueryRequest needExpandMchntListQueryRequest = new NeedExpandMchntListQueryRequest();
        ConvertUtil.convertOutput(needExpandMchntListQueryRequest);
        return needExpandMchntListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        NeedExpandMchntListQueryRequest needExpandMchntListQueryRequest = (NeedExpandMchntListQueryRequest) request;
        NeedExpandMchntListQueryResponse needExpandMchntListQueryResponse = new NeedExpandMchntListQueryResponse();
        BizResponse<NeedExpandMchntListQueryResponse> bizResponse = new BizResponse<>();

        String instId = needExpandMchntListQueryRequest.getHeader().getInstId();
        String userId = needExpandMchntListQueryRequest.getUserId();
        String managerId = needExpandMchntListQueryRequest.getManagerId();
        int pageIndex = needExpandMchntListQueryRequest.getPageIndex();
        int pageSize = needExpandMchntListQueryRequest.getPageSize();
        /* 参数合法性检查 */
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(managerId)) {
            rglog.error("接口上传参数不合法,userId:<{}>,managerId:<{}>", userId, managerId);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 根据拓展经理工号查询商户申请信息 */
        BspsMchntApplyMapper bspsMchntApplyMapper = new BspsMchntApplyMapperImpl();
        List<MchntApplyInfo> mchntApplyInfoList;
        if (-1 == pageIndex || -1 == pageSize) {
            //查询全部
            mchntApplyInfoList = bspsMchntApplyMapper.selectBspsMchntInfoByManagerId(instId, managerId);
        } else {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex);
            pageInfo.setPageSize(pageSize);
            PageResult pageResult = bspsMchntApplyMapper.selectBspsMchntInfoByManagerIdPage(instId, managerId, pageInfo);
            if (null == pageResult || CollectionUtils.isEmpty(pageResult.getResult())) {
                rglog.error("超过最大页数，数据查询为空,userId:<{}>,managerId:<{}>", userId, managerId);
                needExpandMchntListQueryResponse.setTotalPage(0);
                needExpandMchntListQueryResponse.setTotalRows(0);
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setResult(needExpandMchntListQueryResponse);
                return bizResponse;
            } else {
                mchntApplyInfoList = pageResult.getResult().stream().map(item -> (MchntApplyInfo) item).collect(Collectors.toList());
                needExpandMchntListQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                needExpandMchntListQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
                needExpandMchntListQueryResponse.setPageIndex(pageResult.getPageInfo().getPageIndex());
                needExpandMchntListQueryResponse.setPageSize(pageResult.getPageInfo().getPageSize());
            }
        }
        List<NeedExpandMchntInfoBean> needExpandMchntInfoBeanList = new ArrayList<>();
        PublicDealService publicDealService = new PublicDealServiceImpl();
        for (MchntApplyInfo mchntApplyInfo : mchntApplyInfoList) {
            NeedExpandMchntInfoBean needExpandMchntInfoBean = new NeedExpandMchntInfoBean();
            needExpandMchntInfoBean.setManagementScope(mchntApplyInfo.getManagement());
            needExpandMchntInfoBean.setApplyId(mchntApplyInfo.getApplyId());
            needExpandMchntInfoBean.setAddress(mchntApplyInfo.getAddress());
            needExpandMchntInfoBean.setApplyTime(mchntApplyInfo.getApplyTime());
            needExpandMchntInfoBean.setContactName(mchntApplyInfo.getContactName());
            needExpandMchntInfoBean.setContactPhone(mchntApplyInfo.getContactPhone());
            needExpandMchntInfoBean.setMchntName(mchntApplyInfo.getMchntName());
            needExpandMchntInfoBean.setStreet(mchntApplyInfo.getStreet());
            needExpandMchntInfoBean.setManagerId(mchntApplyInfo.getManagerId());
            needExpandMchntInfoBean.setManagerName(mchntApplyInfo.getManagerName());
            needExpandMchntInfoBean.setManagerPhone(mchntApplyInfo.getManagerPhone());
            if (!StringUtil.isEmptyOrNull(mchntApplyInfo.getArea())) {
                needExpandMchntInfoBean.setAreaCode(mchntApplyInfo.getArea());
                needExpandMchntInfoBean.setArea(publicDealService.getRegionName(mchntApplyInfo.getArea()));
            }
            if (!StringUtil.isEmptyOrNull(mchntApplyInfo.getCity())) {
                needExpandMchntInfoBean.setCity(publicDealService.getRegionName(mchntApplyInfo.getCity()));
            }
            if (!StringUtil.isEmptyOrNull(mchntApplyInfo.getProvince())) {
                needExpandMchntInfoBean.setProvince(publicDealService.getRegionName(mchntApplyInfo.getProvince()));
            }
            needExpandMchntInfoBeanList.add(needExpandMchntInfoBean);
        }
        needExpandMchntListQueryResponse.setNeedExpandMchntInfoList(needExpandMchntInfoBeanList);
        /* 按照接口转换基础产品信息并响应给请求方 */
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(needExpandMchntListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        NeedExpandMchntListQueryResponse needExpandMchntListQueryResponse = (NeedExpandMchntListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(needExpandMchntListQueryResponse);
    }


}
